

crosscheck <- function(a,b){
  
out <- list(unique(a[!a%in%b]),unique(b[!b%in%a]), c(a[a%in%b],b[b%in%a]))  

names(out) <-c("only a", "only b", "merged")

return(out)    
}

require(haven)

require(foreign)


####################################### GET ALL THE IDs and Locations


letters <- c("a", "b", "c","d","e","f",
             "g", "h","i", "j","k")

all.nuts.ukhls <- ""

diagnosis <- NA

for( i in 1:length(letters)){
  
    temp <- as.data.frame(read_dta(paste0("./UKDA-6666-stata/stata/stata13/ukhls/",letters[i],"_oslaua_protect.dta")))

    temp$wave <- letters[i]
  
    names(temp)[1:2] <-c("hidp",
                       "oslaua")

    if(i==1){
  
        ukhls.ids <- temp
  
    }else{
  
        ukhls.ids <- rbind(ukhls.ids, temp)
  
        cat(i)
  
    }    

}

lau.ons.2 <- read.csv("./ChanBrexitReplication/Local_Authority_DistrictCW.csv")

names(lau.ons.2)[1]<- c("LAD16CD")

onslau <- read.csv("./ChanBrexitReplication/ONSLau.csv")

tha.merge <- crosscheck(lau.ons.2$LAD16CD, ukhls.ids$oslaua[ukhls.ids$wave=="h"]) 

not.in.using <- tha.merge[[2]]

onslau$NUTS115CD[is.element(not.in.using,onslau$LAU116CD)]

onslau <- unique(onslau[,c(3,5)])

onslau$NUTS2 <- sapply(onslau$NUTS315CD, function(x) substr(x,1,4))

ukhls.ids <- merge(ukhls.ids, lau.ons.2, by.x="oslaua",by.y=1, all.x=TRUE )

ukhls.ids <- ukhls.ids[,c("hidp", "wave","NUTS318CD" ,"NUTS318NM")]

write.dta(ukhls.ids,file="./ChanBrexitReplication/ukhls.ids.dta" )

